package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.AdminBank;
import com.ynsj.domain.Notice;
import com.ynsj.domain.WebConfig;
import com.ynsj.model.R;
import com.ynsj.service.AdminBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(tags = "银行卡管理控制器")
@RequestMapping("/adminBanks")
public class AdminBankController {

    @Autowired
    private AdminBankService adminBankService;

    @GetMapping
    @ApiOperation("分页查询银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "bankCard",value = "银行卡号")
    })
    @PreAuthorize("hasAuthority('admin_bank_query')")
    public R<Page<AdminBank>> findBankPage(@ApiIgnore Page<AdminBank> page, String bankCard){
        return R.ok(adminBankService.findByPage(page,bankCard));
    }

    @PostMapping
    @ApiOperation("新增银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank",value = "银行卡信息的json")
    })
    @PreAuthorize("hasAuthority('admin_bank_create')")
    public R addBank(@RequestBody @Validated AdminBank adminBank){
        boolean isOk = adminBankService.save(adminBank);
        if (isOk){
            return R.ok("新增银行卡信息成功");
        }
        return R.fail("新增银行卡信息失败");
    }

    @PatchMapping
    @ApiOperation("修改银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank",value = "银行卡信息的json")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R updateBank(@RequestBody @Validated AdminBank adminBank){
        boolean isOk = adminBankService.updateById(adminBank);
        if (isOk){
            return R.ok("更新银行卡信息成功");
        }
        return R.fail("更新银行卡信息失败");
    }

    @PostMapping("/adminUpdateBankStatus")
    @ApiOperation("禁用/启用银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "要启用/禁用的银行卡id"),
            @ApiImplicitParam(name = "status",value = "更新的状态")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R updateStatus(Long bankId, Integer status){
        boolean isOk = adminBankService.updateStatus(bankId,status);
        if (isOk){
            return R.ok("禁用/启用成功");
        }
        return R.fail("禁用/启用失败");
    }
}
